<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>

    <!-- 搜索结果列表 -->
    <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :immediate-check="false"
        offset="50"
        @load="onLoad"
      >
        <article-item
          v-for="obj in searchResultList"
          :key="obj.art_id"
          :artObj="obj"
          :isShow="false"
           @click.native="goArticleDetail(obj.art_id)"
        ></article-item>
      </van-list>
    </div>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem.vue'
import { searchResultAPI } from '@/api'
export default {
  components: { ArticleItem },
  name: 'SearchResult',
  data () {
    return {
      searchResultList: [],
      page: 1, // 页码
      loading: false, // 加载状态
      finished: false // 全部加载完成状态
    }
  },
  methods: {
    // 跳转文章详情页
    goArticleDetail (artId) {
      console.log(111)
      this.$router.push({
        path: '/article_detail',
        query: {
          aId: artId
        }
      })
    },
    // 触底加载更多
    onLoad () {
      if (this.searchResultList.length > 0) {
        this.page++
        this.getSearchResultList()
      }
    },
    // 获取 搜索结果列表
    async getSearchResultList () {
      const res = await searchResultAPI({
        keyWords: this.$route.params.keywords,
        page: this.page
      })
      // console.log(res)
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      // 合并数据
      this.searchResultList = [...this.searchResultList, ...res.data.data.results]
      this.loading = false
    }
  },
  created () {
    this.getSearchResultList()
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
